{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This example is kindly contributed by [FreddyBaudine](https://github.com/FreddyBaudine) for reproducing [pygae/galgebra#15](https://github.com/pygae/galgebra/issues/15) with modifications by [utensil](https://github.com/utensil)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "from galgebra.printer import  Format, xpdf, Fmt\n",
    "Format()\n",
    "from sympy import symbols, Rational, latex\n",
    "from galgebra.ga import Ga"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# st4: Spacetime Algebra I\n",
    "st4coords = (t,x,y,z) = symbols('t x y z', real=True) \n",
    "st4 = Ga('e_t e_x e_y e_z', g=[1,-1,-1,-1], coords=st4coords)\n",
    "(e_t,e_x,e_y,e_z) = st4.mv()\n",
    "(e__t,e__x,e__y,e__z) = st4.mvr(norm = False) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "Format(Fmode = True, Dmode = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "(grad,rgrad) = st4.grads()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = st4.mv('s','scalar',f = True)\n",
    "a = st4.mv('a','vector',f = True)\n",
    "b = st4.mv('b','vector',f = True)\n",
    "B = st4.mv('B','bivector',f = True)\n",
    "C = st4.mv('C',3)\n",
    "I = st4.mv('I','pseudo')\n",
    "S = st4.mv('S','spinor',f = True)\n",
    "M = st4.mv('M','mv',f = True)\n",
    "\n",
    "M1inv = M.grade(1).inv()\n",
    "M3inv = M.grade(3).inv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} \\mathbf{a} = a^{t}  \\boldsymbol{e}_{t} + a^{x}  \\boldsymbol{e}_{x} + a^{y}  \\boldsymbol{e}_{y} + a^{z}  \\boldsymbol{e}_{z} \\end{equation*}"
      ],
      "text/plain": [
       "\\mathbf{a} = a__t*e_t + a__x*e_x + a__y*e_y + a__z*e_z"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.Fmt(1,r'\\mathbf{a}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} \\mathbf{a} \\mathbf{a} = {a^{t} }^{2} - {a^{x} }^{2} - {a^{y} }^{2} - {a^{z} }^{2} \\end{equation*}"
      ],
      "text/plain": [
       "\\mathbf{a} \\mathbf{a} = a__t**2 - a__x**2 - a__y**2 - a__z**2"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(a*a).Fmt(2,r'\\mathbf{a} \\mathbf{a}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} \\mathbf{a}^{-1} =  \\begin{aligned}[t]  & \\frac{a^{t} }{{a^{t} }^{2} - {a^{x} }^{2} - {a^{y} }^{2} - {a^{z} }^{2}} \\boldsymbol{e}_{t} \\\\  &  + \\frac{a^{x} }{{a^{t} }^{2} - {a^{x} }^{2} - {a^{y} }^{2} - {a^{z} }^{2}} \\boldsymbol{e}_{x} \\\\  &  + \\frac{a^{y} }{{a^{t} }^{2} - {a^{x} }^{2} - {a^{y} }^{2} - {a^{z} }^{2}} \\boldsymbol{e}_{y} \\\\  &  + \\frac{a^{z} }{{a^{t} }^{2} - {a^{x} }^{2} - {a^{y} }^{2} - {a^{z} }^{2}} \\boldsymbol{e}_{z}  \\end{aligned}  \\end{equation*}"
      ],
      "text/plain": [
       "\\mathbf{a}^{-1} =  a__t*e_t/(a__t**2 - a__x**2 - a__y**2 - a__z**2)\n",
       " + a__x*e_x/(a__t**2 - a__x**2 - a__y**2 - a__z**2)\n",
       " + a__y*e_y/(a__t**2 - a__x**2 - a__y**2 - a__z**2)\n",
       " + a__z*e_z/(a__t**2 - a__x**2 - a__y**2 - a__z**2)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.inv().Fmt(3,r'\\mathbf{a}^{-1}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} \\langle \\mathbf{M} \\rangle _1 \\langle \\mathbf{M} \\rangle _1 = {M^{t} }^{2} - {M^{x} }^{2} - {M^{y} }^{2} - {M^{z} }^{2} \\end{equation*}"
      ],
      "text/plain": [
       "\\langle \\mathbf{M} \\rangle _1 \\langle \\mathbf{M} \\rangle _1 = M__t**2 - M__x**2 - M__y**2 - M__z**2"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(M.grade(1) * M.grade(1)).\\\n",
    " Fmt(1,r'\\langle \\mathbf{M} \\rangle _1 \\langle \\mathbf{M} \\rangle _1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} \\langle \\mathbf{M} \\rangle _1 ^{-1} =  \\begin{aligned}[t]  & \\frac{M^{t} }{{M^{t} }^{2} - {M^{x} }^{2} - {M^{y} }^{2} - {M^{z} }^{2}} \\boldsymbol{e}_{t} \\\\  &  + \\frac{M^{x} }{{M^{t} }^{2} - {M^{x} }^{2} - {M^{y} }^{2} - {M^{z} }^{2}} \\boldsymbol{e}_{x} \\\\  &  + \\frac{M^{y} }{{M^{t} }^{2} - {M^{x} }^{2} - {M^{y} }^{2} - {M^{z} }^{2}} \\boldsymbol{e}_{y} \\\\  &  + \\frac{M^{z} }{{M^{t} }^{2} - {M^{x} }^{2} - {M^{y} }^{2} - {M^{z} }^{2}} \\boldsymbol{e}_{z}  \\end{aligned}  \\end{equation*}"
      ],
      "text/plain": [
       "\\langle \\mathbf{M} \\rangle _1 ^{-1} =  M__t*e_t/(M__t**2 - M__x**2 - M__y**2 - M__z**2)\n",
       " + M__x*e_x/(M__t**2 - M__x**2 - M__y**2 - M__z**2)\n",
       " + M__y*e_y/(M__t**2 - M__x**2 - M__y**2 - M__z**2)\n",
       " + M__z*e_z/(M__t**2 - M__x**2 - M__y**2 - M__z**2)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "M1inv.Fmt(3,r'\\langle \\mathbf{M} \\rangle _1 ^{-1}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} \\langle \\mathbf{M} \\rangle _3 = M^{txy}  \\boldsymbol{e}_{t}\\wedge \\boldsymbol{e}_{x}\\wedge \\boldsymbol{e}_{y} + M^{txz}  \\boldsymbol{e}_{t}\\wedge \\boldsymbol{e}_{x}\\wedge \\boldsymbol{e}_{z} + M^{tyz}  \\boldsymbol{e}_{t}\\wedge \\boldsymbol{e}_{y}\\wedge \\boldsymbol{e}_{z} + M^{xyz}  \\boldsymbol{e}_{x}\\wedge \\boldsymbol{e}_{y}\\wedge \\boldsymbol{e}_{z} \\end{equation*}"
      ],
      "text/plain": [
       "\\langle \\mathbf{M} \\rangle _3 =  M__txy*e_t^e_x^e_y + M__txz*e_t^e_x^e_z + M__tyz*e_t^e_y^e_z + M__xyz*e_x^e_y^e_z"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "M.grade(3).Fmt(2,r'\\langle \\mathbf{M} \\rangle _3')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} \\langle \\mathbf{M} \\rangle _3 \\langle \\mathbf{M} \\rangle _3 = - {M^{txy} }^{2} - {M^{txz} }^{2} - {M^{tyz} }^{2} + {M^{xyz} }^{2} \\end{equation*}"
      ],
      "text/plain": [
       "\\langle \\mathbf{M} \\rangle _3 \\langle \\mathbf{M} \\rangle _3 = -M__txy**2 - M__txz**2 - M__tyz**2 + M__xyz**2"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(M.grade(3) * M.grade(3)).\\\n",
    " Fmt(1,r'\\langle \\mathbf{M} \\rangle _3 \\langle \\mathbf{M} \\rangle _3')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} \\langle \\mathbf{M} \\rangle _3^{-1} =  \\begin{aligned}[t]  & - \\frac{M^{txy} }{{M^{txy} }^{2} + {M^{txz} }^{2} + {M^{tyz} }^{2} - {M^{xyz} }^{2}} \\boldsymbol{e}_{t}\\wedge \\boldsymbol{e}_{x}\\wedge \\boldsymbol{e}_{y} \\\\  &  - \\frac{M^{txz} }{{M^{txy} }^{2} + {M^{txz} }^{2} + {M^{tyz} }^{2} - {M^{xyz} }^{2}} \\boldsymbol{e}_{t}\\wedge \\boldsymbol{e}_{x}\\wedge \\boldsymbol{e}_{z} \\\\  &  - \\frac{M^{tyz} }{{M^{txy} }^{2} + {M^{txz} }^{2} + {M^{tyz} }^{2} - {M^{xyz} }^{2}} \\boldsymbol{e}_{t}\\wedge \\boldsymbol{e}_{y}\\wedge \\boldsymbol{e}_{z} \\\\  &  - \\frac{M^{xyz} }{{M^{txy} }^{2} + {M^{txz} }^{2} + {M^{tyz} }^{2} - {M^{xyz} }^{2}} \\boldsymbol{e}_{x}\\wedge \\boldsymbol{e}_{y}\\wedge \\boldsymbol{e}_{z}  \\end{aligned}  \\end{equation*}"
      ],
      "text/plain": [
       "\\langle \\mathbf{M} \\rangle _3^{-1} =  -M__txy*e_t^e_x^e_y/(M__txy**2 + M__txz**2 + M__tyz**2 - M__xyz**2)\n",
       " - M__txz*e_t^e_x^e_z/(M__txy**2 + M__txz**2 + M__tyz**2 - M__xyz**2)\n",
       " - M__tyz*e_t^e_y^e_z/(M__txy**2 + M__txz**2 + M__tyz**2 - M__xyz**2)\n",
       " - M__xyz*e_x^e_y^e_z/(M__txy**2 + M__txz**2 + M__tyz**2 - M__xyz**2)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "M3inv.Fmt(3,r'\\langle \\mathbf{M} \\rangle _3^{-1}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
